package com.hans.myscanner.Scanner.Activity;


import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.widget.TabHost;

import com.hans.myscanner.R;
import com.hans.myscanner.widget.CustomFontTextView;
import com.hans.myscanner.widget.FooterNavBar;

public class MainFrameActivity extends TabActivity {
    private TabHost tabHost;
    private FooterNavBar footerNavBar;

    private static final String TAG_TAB_SCAN = "SCAN";
    private static final String TAG_TAB_HISTORY = "HISTORY";
    private static final String TAG_TAB_MAKE = "MAKE";
    private static final String TAG_TAB_SETTINGS = "SETTINGS";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_tabhost);
        findView();
        initView();
    }

    private void findView(){
        tabHost=(TabHost) findViewById(android.R.id.tabhost);
        footerNavBar = (FooterNavBar) findViewById(R.id.footer_nav_bar);
    }

    private void initView(){
        footerNavBar.setOnNavItemChangedListener(onNavItemChangedListener);
        footerNavBar.setCurrentPosition(1);

        if(tabHost == null) throw new NullPointerException("tabHost为啥子就为NULL了");

        tabHost.setup(getLocalActivityManager());
        tabHost.addTab(tabHost.newTabSpec(TAG_TAB_SCAN).setContent(new Intent(MainFrameActivity.this, ScanSubActivity.class)).setIndicator(TAG_TAB_SCAN));
        tabHost.addTab(tabHost.newTabSpec(TAG_TAB_HISTORY).setContent(new Intent(MainFrameActivity.this,HistorySubActivity.class)).setIndicator(TAG_TAB_HISTORY));
        tabHost.addTab(tabHost.newTabSpec(TAG_TAB_MAKE).setContent(new Intent(MainFrameActivity.this,MakeSubActivity.class)).setIndicator(TAG_TAB_MAKE));
        tabHost.addTab(tabHost.newTabSpec(TAG_TAB_SETTINGS).setContent(new Intent(MainFrameActivity.this,SettingsSubActivity.class)).setIndicator(TAG_TAB_SETTINGS));
        tabHost.setCurrentTabByTag(TAG_TAB_HISTORY);
    }


    private FooterNavBar.OnNavItemChangedListener onNavItemChangedListener = new FooterNavBar.OnNavItemChangedListener() {
        private static final int FIRST_TAB = 0;
        private static final int SECOND_TAB = 1;
        private static final int THIRD_TAB = 2;
        private static final int FOURTH_TAB = 3;

        private static final float UNCHECKED_ALPHA =0.5f;//设置透明度
        private static final float CHECKED_ALPHA =1.0f;
        @Override
        public void onNavItemChanged(CustomFontTextView[] items,CustomFontTextView item, int posistion) {
            for(int i = 0; i < 4; i++) items[i].setAlpha(UNCHECKED_ALPHA);
            switch (posistion){
                case FIRST_TAB:
                    tabHost.setCurrentTabByTag(TAG_TAB_SCAN);
                    item.setAlpha(CHECKED_ALPHA);
                    break;
                case SECOND_TAB:
                    tabHost.setCurrentTabByTag(TAG_TAB_HISTORY);
                    item.setAlpha(CHECKED_ALPHA);
                    break;

                case THIRD_TAB:
                    tabHost.setCurrentTabByTag(TAG_TAB_MAKE);
                    item.setAlpha(CHECKED_ALPHA);
                    break;

                case FOURTH_TAB:
                    tabHost.setCurrentTabByTag(TAG_TAB_SETTINGS);
                    item.setAlpha(CHECKED_ALPHA);
                    break;
            }
        }
    };
}

